package com.example.bakend.controller;

import com.example.bakend.pojo.Book;
import com.example.bakend.pojo.Reader;
import com.example.bakend.pojo.Result;
import com.example.bakend.service.ReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("reader")
public class ReaderController {
    @Autowired
    ReaderService readerService;

    @GetMapping
    public Result selectReaders(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(name = "pageSize", defaultValue = "20") Integer pageSize) {
        return Result.success(readerService.selectReaders(pageNum, pageSize));
    }

    @GetMapping("{readerId}")
    public Result selectReader(@PathVariable Integer id) {
        return Result.success(readerService.selectReader(id));
    }

    @PostMapping
    public Result insertReader(@RequestBody Reader reader) {
//        校验名字重复,校验联系方式重复
        List<Reader> readers = readerService.selectReaders();
        for (int i = 0; i < readers.size(); i++) {
            if (readers.get(i).getName().equals(reader.getName())) {
                return Result.error("名字重复");
            }
            if (readers.get(i).getContact().equals(reader.getContact())) {
                return Result.error("联系方式重复");
            }
        }
//        校验年龄
        if (reader.getAge() <= 0 || reader.getAge() >= 65) {
            return Result.error("您输入的年龄不合法");
        }

//        校验手机号位数
        if (reader.getContact().length() != 11) {
            return Result.error("请输入正确手机号码");
        }

        return Result.success(readerService.insertReader(reader));
    }

    @PutMapping
    public Result updateReader(@RequestBody Reader reader) {
        //        校验名字重复,校验联系方式重复
        List<Reader> readers = readerService.selectReaders();
        for (int i = 0; i < readers.size(); i++) {
//            假如id相等，会跳过判断
            if (readers.get(i).getName().equals(reader.getName()) && !readers.get(i).getId().equals(reader.getId())) {
                return Result.error("名字重复");
            }
            if (readers.get(i).getContact().equals(reader.getContact()) && !readers.get(i).getId().equals(reader.getId())) {
                return Result.error("联系方式重复");
            }
        }
//        校验年龄
        if (reader.getAge() <= 0 || reader.getAge() >= 65) {
            return Result.error("您输入的年龄不合法");
        }
//        校验年龄类型
        if (Math.round(reader.getAge()) != reader.getAge()) {
            return Result.error("年龄必须是整数");
        }
//        校验手机号位数
        if (reader.getContact().length() != 11) {
            return Result.error("请输入正确手机号码");
        }
        return Result.success(readerService.updateReader(reader));
    }

    @DeleteMapping("{ids}")
    public Result deleteReaders(@PathVariable List<Integer> ids) {
        return Result.success(readerService.deleteReaders(ids));
    }

    @GetMapping("tiaojian")
    public Result getReaders(@RequestParam(name = "name", required = false) String name,
                             @RequestParam(name = "gender", required = false) String gender,
                             @RequestParam(name = "contact", required = false) String contact,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTimeBegin,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTimeEnd,
                             @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize) {
        return Result.success(readerService.getReaders(name, gender, contact, updateTimeBegin, updateTimeEnd, pageNum, pageSize));
    }

}
